package com.af.community.smart.business

import android.os.Bundle
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.getString
import com.af.community.smart.base.oneClick
import com.af.community.smart.model.HouseData
import com.af.community.smart.model.PeopleData
import com.af.community.smart.utils.ActivityStack
import com.af.community.smart.utils.DialogHelper.showListDialog
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.subscribeByNoError
import com.af.community.smart.utils.trimString
import com.af.community.smart.viewmodel.PatrolViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_patrol_add.*
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class PatrolAddActivity : BaseActivity() {

    private var peopleData: PeopleData? = null
    private var houseData: HouseData? = null
    private var mResult = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_patrol_add)
        initTitle(title = "巡检结果")
    }

    override fun getViewModel() = getViewModel<PatrolViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        peopleData = intent.getSerializableExtra("people") as PeopleData?
        houseData = intent.getSerializableExtra("house") as HouseData?

        patrol_result.oneClick {
            showListDialog(
                "正常", "异常"
            ) { index, hint ->
                mResult = if (index == 0) "1" else "2"
                patrol_result.setRightString(hint)
            }
        }

        bt_submit.oneClick {
            when {
                mResult.isEmpty() -> {
                    toast("请选择巡检结果")
                    return@oneClick
                }
                patrol_memo.text.isBlank() -> {
                    toast("请输入备注信息")
                    return@oneClick
                }
            }

            when {
                peopleData != null -> {
                    getViewModel().addPeopleData(
                        "userId" to getString("userId"),
                        "type" to 1,
                        "result" to mResult,
                        "remark" to patrol_memo.trimString(),
                        "identity" to peopleData?.account.orEmpty(),
                        "name" to peopleData?.userName.orEmpty(),
                        "sex" to peopleData?.sex.orEmpty(),
                        "nation" to peopleData?.national.orEmpty(),
                        "housePhone" to peopleData?.phone.orEmpty(),
                        "bisId" to peopleData?.id.orEmpty()
                    )
                        .bindLifeCycle(getLifecycleOwner())
                        .subscribeByNoError(
                            event = getViewModel(),
                            onSuccess = {
                                toast("巡检结果添加成功！")
                                LiveEventBus.get("patrol_add").post("done")
                                ActivityStack.screenManager.popActivities(
                                    this@PatrolAddActivity::class.java,
                                    PatrolPeopleScanActivity::class.java,
                                    PatrolPeopleDetialActivity::class.java
                                )
                            }
                        )
                }
                houseData != null -> {
                    val param = HashMap<String, Any>()
                    param["userId"] = getString("userId")
                    param["type"] = 2
                    param["result"] = mResult
                    param["remark"] = patrol_memo.trimString()
                    param["bisId"] = houseData?.id.orEmpty()
                    param["houseType"] = houseData?.houseType.orEmpty()
                    param["name"] = houseData?.houseTypeName.orEmpty()
                    when(houseData?.houseType) {
                        "ht_zf" -> {
                            param["houseContacts"] = houseData?.ownerName.orEmpty()
                            param["housePhone"] = houseData?.ownerCredit.orEmpty()
                            param["houseAddress"] = houseData?.houseTypeName.orEmpty()
                        }
                        "ht_czf" -> {
                            param["houseContacts"] = houseData?.ownerName.orEmpty()
                            param["housePhone"] = houseData?.phone.orEmpty()
                            param["houseAddress"] = houseData?.houseTypeName.orEmpty()
                        }
                        else -> {
                            param["houseContacts"] = houseData?.personCharge.orEmpty()
                            param["housePhone"] = houseData?.phone.orEmpty()
                            param["houseAddress"] = houseData?.name.orEmpty()
                        }
                    }

                    getViewModel().addHouseData(param )
                        .bindLifeCycle(getLifecycleOwner())
                        .subscribeByNoError(
                            event = getViewModel(),
                            onSuccess = {
                                toast("巡检结果添加成功！")
                                LiveEventBus.get("patrol_house").post("done")
                                ActivityStack.screenManager.popActivities(
                                    this@PatrolAddActivity::class.java,
                                    HouseDetailActivity::class.java
                                )
                            }
                        )
                }
            }
        }
    }

}
